import Vue from "vue";
import Vuex from "vuex";
import * as api from "../api"
import allRoutes from '../router/allRoutes'
import dynamicRoutes from '../router/dynamicRoutes'
import recursionRoutes from '../utils/recursionRoutes'
import router from "../router"
Vue.use(Vuex);
let userInfo = localStorage.getItem("qf-userInfo") || "{}"
export default new Vuex.Store({
  state: {
    updatauserdata:"",//修改用户数据回显暂存
    show:"",//用来控制修改账号组件的开关
    userInfo:JSON.parse(userInfo),//用户信息
    sideMenu:[],//侧边栏需要展示的配置路由计算结果
    breadCrumb:[],//面包屑数据
    dialogVisible:false,//控制dialog显示隐藏属性
    dialogTitle:"增加学员信息",//控制dialog标题显示文字
    stuData:[],//学员数据
    loading:true,//控制加载框动画的字段
    currentRow:{},//dialog框修改回显的数据
    stuTotal:0,//总条目
    stuPageSize:10,//每页显示数量
  },
  getters: {},
  mutations: {
    //传递回显的数据
    SET_UPDATADATA(state,payload){
      state.updatauserdata = payload
    },
    //控制账号修改组件框的开关
    SET_UPDATAUSERS(state,payload){
      state.show = payload
    },
    //控制加载框关闭
    SET_LOAD(state){
      state.loading=false;
    },
    //设置学员项目管理表格的数据
      SET_STU_DATA(state,payload){
      state.stuData=payload.data;
      //设置学员总条目
      state.stuTotal=payload.total
      console.log(payload.total);
    },
    //显示dialog
    SHOW_EDIALOG(state){
      state.dialogVisible=true;
    },
    //隐藏dialog
    HIDE_DIALOG(state){
      state.dialogVisible=false;
    },
    //更改dialog标题文字
    CHANGE_EDIALOG_TITLE(state,payload){
      state.dialogTitle=payload;
    },
    //保存用户信息
    SET_USER_INFO(state,payload){
      state.userInfo = payload
    },
    SET_MENULIST(state,payload){
      // state.sideMenu = payload
      //将用户需求路由动态添加到router配置中
      //router.addRoutes([])添加路由的方法
      //添加到dynamicRoutes中/路径的children里面
      let target = dynamicRoutes.find(item=>item.path==="/")
      target.children = [...payload,...target.children]
      router.addRoutes(dynamicRoutes)
      //设置需要排除掉的路由数组
      let excludeRoutes = ["preorder","payment"]
      //过滤掉预支付订单页面的路由，不在侧边栏显示
      target.children = target.children.filter(item=>{
        return !excludeRoutes.includes(item.path)
      })
      state.sideMenu = target.children
    },
    SET_BREADCRUMB(state,payload){//设置面包屑数据
      state.breadCrumb = payload
    },
    SET_CURRENT_ROW(state,payload){//修改dialog回显数据
      state.currentRow = {...payload}
    },
  },
  actions: {
    async GET_MENULIST({commit}){//获取用户菜单
      let res = await api.getMenulisrApi()
      let {menuList} = res.data
      //使用recursionRoutes方法计算出用户需要的路由，提交给mutation
      let userRoutes = recursionRoutes(menuList,allRoutes)
      commit("SET_MENULIST",userRoutes)
    },
    GET_STULIST(conent,payload={}){//获取学员列表
      let {page=1,count=10,classes=""} = payload;//解构传递的参数
      //让请求的总数和每一页展示条目相等
      conent.state.stuPageSize = count
      
      //判断传入的值是否大于后台总条数，如果大于则是count=总条数
      // console.log(conent.state);
      // if(conent.state.stuTotal>count){
      //   count=conent.state.stuTotal
      // }

      api.getStuListApi(page,count,classes)
      .then(res=>{
        conent.commit("SET_STU_DATA",res.data)
        // console.log("这是total"+conent.state.stuTotal);
        // console.log("这是count"+count);
        // if(conent.state.stuTotal>count){
        //   count=conent.state.stuTotal
        //   api.getStuListApi(page,count,classes)
        //   .then(res=>{
        //     conent.commit("SET_STU_DATA",res.data)
        //   })
        // }
        //得到数据后关闭加载框
        conent.commit("SET_LOAD")
      })
    }
  },
  modules: {},
});
